#import "scene_comic.h"

#define COMIC_PIC_FADE_IN_TIME 1.0f
#define COMIC_PIC_WAIT_TIME 2.0f

#if (HARD_WARE == HARD_WARE_IPHONE)
	#define COMIC_SKIP_SIZE 24
#elif (HARD_WARE == HARD_WARE_IPAD)
	#define COMIC_SKIP_SIZE 48
#endif

@implementation SceneComic

- (id)initSceneComic
{
	int i;
	CCMenuItemLabel *_MenuItemSkip;

	if ((self = [super init]))
	{
		self.isTouchEnabled = YES;
		flagTouchLock_ = false;
		flagFunctionLock_ = false;

		/*color
		*/
		color_.r = 232;
		color_.g = 120;
		color_.b = 132;

		/*comic page
		*/
		if (1 == g_ChapterMissionID)
		{
			/*page 1
			*/
			if (g_ChpaterComicPrev)
			{
				for (i = 0; i < 7; i++)
				{
					page_1_[i] = [CCSprite spriteWithFile:[NSString stringWithFormat:@"1-%d.png", i + 1]];
					[self addChild:page_1_[i]];
					[page_1_[i] runAction:[CCHide action]];
					page_1_[i].opacity = 0;
					page_1_[i].positionInPixels = g_CfgComicPage1Position[i];
				}
				[self ActionComic:1];
				goto CREATE_MENU_;
			}
			else
			{
				[self ComicOver];
				goto OVER_;
			}
		}
		else if (5 == g_ChapterMissionID)
		{
			if (g_ChpaterComicPrev)
			{
				/*page 2
				*/
				for (i = 0; i < 6; i++)
				{
					page_2_[i] = [CCSprite spriteWithFile:[NSString stringWithFormat:@"2-%d.png", i + 1]];
					[self addChild:page_2_[i]];
					[page_2_[i] runAction:[CCHide action]];
					page_2_[i].opacity = 0;
					page_2_[i].positionInPixels = g_CfgComicPage2Position[i];
				}
				[self ActionComic:2];
			}
			else
			{
				/*page 3
				*/
				for (i = 0; i < 5; i++)
				{
					page_3_[i] = [CCSprite spriteWithFile:[NSString stringWithFormat:@"3-%d.png", i + 1]];
					[self addChild:page_3_[i]];
					[page_3_[i] runAction:[CCHide action]];
					page_3_[i].opacity = 0;
					page_3_[i].positionInPixels = g_CfgComicPage3Position[i];
				}
				[self ActionComic:3];
			}
			goto CREATE_MENU_;
		}
		else if (10 == g_ChapterMissionID)
		{
			if (g_ChpaterComicPrev)
			{
				/*page 4
				*/
				for (i = 0; i < 7; i++)
				{
					page_4_[i] = [CCSprite spriteWithFile:[NSString stringWithFormat:@"4-%d.png", i + 1]];
					[self addChild:page_4_[i]];
					[page_4_[i] runAction:[CCHide action]];
					page_4_[i].opacity = 0;
					page_4_[i].positionInPixels = g_CfgComicPage4Position[i];
				}
				[self ActionComic:4];
			}
			else
			{
				/*page 5
				*/
				for (i = 0; i < 4; i++)
				{
					page_5_[i] = [CCSprite spriteWithFile:[NSString stringWithFormat:@"5-%d.png", i + 1]];
					[self addChild:page_5_[i]];
					[page_5_[i] runAction:[CCHide action]];
					page_5_[i].opacity = 0;
					page_5_[i].positionInPixels = g_CfgComicPage5Position[i];
				}
				[self ActionComic:5];
			}
			goto CREATE_MENU_;
		}
		else if (15 == g_ChapterMissionID)
		{
			if (g_ChpaterComicPrev)
			{
				/*page 6
				*/
				for (i = 0; i < 4; i++)
				{
					page_6_[i] = [CCSprite spriteWithFile:[NSString stringWithFormat:@"6-%d.png", i + 1]];
					[self addChild:page_6_[i]];
					[page_6_[i] runAction:[CCHide action]];
					page_6_[i].opacity = 0;
					page_6_[i].positionInPixels = g_CfgComicPage6Position[i];
				}
				[self ActionComic:6];
			}
			else
			{
				/*page 7
				*/
				for (i = 0; i < 4; i++)
				{
					page_7_[i] = [CCSprite spriteWithFile:[NSString stringWithFormat:@"7-%d.png", i + 1]];
					[self addChild:page_7_[i]];
					[page_7_[i] runAction:[CCHide action]];
					page_7_[i].opacity = 0;
					page_7_[i].positionInPixels = g_CfgComicPage7Position[i];
				}
				[self ActionComic:7];
			}
			goto CREATE_MENU_;
		}
		else
		{
			[self ComicOver];
			goto OVER_;
		}

CREATE_MENU_:
		/*menu
		*/
		_MenuItemSkip = [CCMenuItemLabel itemWithLabel:[CCLabelTTF labelWithString:@"SKIP" fontName:@"font_treasure.ttf" fontSize:COMIC_SKIP_SIZE] target:self selector:@selector(CallBackSkip:)];
		_MenuItemSkip.positionInPixels = ccp(g_ScrrenSize.width - _MenuItemSkip.contentSizeInPixels.width / 2, _MenuItemSkip.contentSizeInPixels.height / 2);
		uiNodeAddMenu(self, CGPointZero, _MenuItemSkip, nil);
	}

OVER_:
	return self;
}

- (void)draw
{
	UICenterRect _Rect;

	_Rect.center = g_ScrrenCenter;
	_Rect.size = g_ScrrenSize;
	glColor4ub(color_.r, color_.g, color_.b, 1);
	uiRenderRect(_Rect);
}

- (void)ccTouchesEnded:(NSSet *)Touch withEvent:(UIEvent *)Event
{
	if (flagTouchLock_)
	{
		return;
	}

	switch (activePageIndex_)
	{
	case 1:
		[page_1_[activePageCount_ - 1] stopAllActions];
		page_1_[activePageCount_ - 1].opacity = 255;
		[self CallBackPage1FadeInOver:nil];
		break;
	case 2:
		[page_2_[activePageCount_ - 1] stopAllActions];
		page_2_[activePageCount_ - 1].opacity = 255;
		[self CallBackPage2FadeInOver:nil];
		break;
	case 3:
		[page_3_[activePageCount_ - 1] stopAllActions];
		page_3_[activePageCount_ - 1].opacity = 255;
		[self CallBackPage3FadeInOver:nil];
		break;
	case 4:
		[page_4_[activePageCount_ - 1] stopAllActions];
		page_4_[activePageCount_ - 1].opacity = 255;
		[self CallBackPage4FadeInOver:nil];
		break;
	case 5:
		[page_5_[activePageCount_ - 1] stopAllActions];
		page_5_[activePageCount_ - 1].opacity = 255;
		[self CallBackPage5FadeInOver:nil];
		break;
	case 6:
		[page_6_[activePageCount_ - 1] stopAllActions];
		page_6_[activePageCount_ - 1].opacity = 255;
		[self CallBackPage6FadeInOver:nil];
		break;
	case 7:
		[page_7_[activePageCount_ - 1] stopAllActions];
		page_7_[activePageCount_ - 1].opacity = 255;
		[self CallBackPage7FadeInOver:nil];
		break;
	default:;
	}
}

- (void)tickSceneChangeToComic:(ccTime)Time
{
	SceneDummy *sceneDummy;

	[self unschedule:@selector(tickSceneChangeToMenu:)];

	sceneDummy = [[SceneDummy alloc] initSceneDummy:SCENE_CHANGE_TO_COMIC];
	[[CCDirector sharedDirector] replaceScene:sceneDummy];
	[sceneDummy release];
}

- (void)tickSceneChangeToMenu:(ccTime)Time
{
	SceneDummy *sceneDummy;

	[self unschedule:@selector(tickSceneChangeToMenu:)];

	sceneDummy = [[SceneDummy alloc] initSceneDummy:SCENE_CHANGE_TO_MENU];
	[[CCDirector sharedDirector] replaceScene:sceneDummy];
	[sceneDummy release];
	sceneDummy.menuState = SCENE_MENU_STATE_MAIN;
}

- (void)tickSceneChangeToChapter:(ccTime)Time
{
	SceneDummy *sceneDummy;

	[self unschedule:@selector(tickSceneChangeToChapter:)];

	sceneDummy = [[SceneDummy alloc] initSceneDummy:SCENE_CHANGE_TO_MENU];
	[[CCDirector sharedDirector] replaceScene:sceneDummy];
	[sceneDummy release];
	sceneDummy.menuState = SCENE_MENU_STATE_CHAPTER;
}

- (void)tickSceneChangeToGame:(ccTime)Time
{
	SceneDummy *sceneDummy;

	[self unschedule:@selector(tickSceneChangeToGame:)];

	sceneDummy = [[SceneDummy alloc] initSceneDummy:SCENE_CHANGE_TO_GAME];
	[[CCDirector sharedDirector] replaceScene:sceneDummy];
	[sceneDummy release];
}

- (void)SceneChangeToComic
{
	SpriteDummy *_Loading;

	/*free child
	*/
	[self removeAllChildrenWithCleanup:TRUE];
	[self stopAllActions];

	/*color
	*/
	memset(&color_, 0, sizeof color_);

	/*loading image
	*/
	_Loading = [[SpriteDummy alloc] initSpriteDummy:false];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToComic:) interval:1.0f];
}

- (void)SceneChangeToMenu
{
	SpriteDummy *_Loading;

	/*free child
	*/
	[self removeAllChildrenWithCleanup:TRUE];
	[self stopAllActions];

	/*color
	*/
	memset(&color_, 0, sizeof color_);

	/*loading image
	*/
	_Loading = [[SpriteDummy alloc] initSpriteDummy:false];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToMenu:) interval:1.0f];
}

- (void)SceneChangeToChapter
{
	SpriteDummy *_Loading;

	/*free child
	*/
	[self removeAllChildrenWithCleanup:TRUE];
	[self stopAllActions];

	/*color
	*/
	memset(&color_, 0, sizeof color_);

	/*loading image
	*/
	_Loading = [[SpriteDummy alloc] initSpriteDummy:false];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToChapter:) interval:1.0f];
}

- (void)SceneChangeToGame
{
	SpriteDummy *_Loading;

	/*free child
	*/
	[self removeAllChildrenWithCleanup:TRUE];
	[self stopAllActions];

	/*color
	*/
	memset(&color_, 0, sizeof color_);

	/*loading image
	*/
	_Loading = [[SpriteDummy alloc] initSpriteDummy:false];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToGame:) interval:1.0f];
}

- (void)ComicOver
{
	if (flagFunctionLock_)
	{
		return;
	}

	flagTouchLock_ = true;
	flagFunctionLock_ = true;

	/*flag
	*/
	if (g_ChpaterComicPrev)
	{
		g_ChpaterComicPrev = false;
		[self SceneChangeToGame];
	}
	else
	{
		g_ChpaterComicPrev = true;

		/*check rate
		*/
		if (10 == g_ChapterMissionID)
		{
			g_FlagCheckRate = true;
		}

		if (15 == g_ChapterMissionID)
		{
			[self SceneChangeToMenu];
		}
		else if (-1 == g_ChapterNextMissionID)
		{
			[self SceneChangeToChapter];
		}
		else
		{
			g_ChapterMissionID = g_ChapterNextMissionID;
			g_ChapterNextMissionID = -1;
			[self SceneChangeToComic];
		}
	}
}

- (void)ActionComic:(int)PageIndex
{
	id _FadeIn;

	activePageIndex_ = PageIndex;
	_FadeIn = [CCFadeIn actionWithDuration:COMIC_PIC_FADE_IN_TIME];
	switch (activePageIndex_)
	{
	case 1:
		[page_1_[activePageCount_] runAction:[CCShow action]];
		[page_1_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage1FadeInOver:))];
		break;
	case 2:
		[page_2_[activePageCount_] runAction:[CCShow action]];
		[page_2_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage2FadeInOver:))];
		break;
	case 3:
		[page_3_[activePageCount_] runAction:[CCShow action]];
		[page_3_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage3FadeInOver:))];
		break;
	case 4:
		[page_4_[activePageCount_] runAction:[CCShow action]];
		[page_4_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage4FadeInOver:))];
		break;
	case 5:
		[page_5_[activePageCount_] runAction:[CCShow action]];
		[page_5_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage5FadeInOver:))];
		break;
	case 6:
		[page_6_[activePageCount_] runAction:[CCShow action]];
		[page_6_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage6FadeInOver:))];
		break;
	case 7:
		[page_7_[activePageCount_] runAction:[CCShow action]];
		[page_7_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage7FadeInOver:))];
		break;
	default:;
	}
	activePageCount_++;
}

- (void)CallBackPage1FadeInOver:(id)Sender
{
	id _FadeIn;

	if (activePageCount_ < 7)
	{
		_FadeIn = [CCFadeIn actionWithDuration:COMIC_PIC_FADE_IN_TIME];
		[page_1_[activePageCount_] runAction:[CCShow action]];
		[page_1_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage1FadeInOver:))];
		activePageCount_++;
	}
	else
	{
		[self ComicOver];
	}
}

- (void)CallBackPage2FadeInOver:(id)Sender
{
	id _FadeIn;

	if (activePageCount_ < 6)
	{
		_FadeIn = [CCFadeIn actionWithDuration:COMIC_PIC_FADE_IN_TIME];
		[page_2_[activePageCount_] runAction:[CCShow action]];
		[page_2_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage2FadeInOver:))];
		activePageCount_++;
	}
	else
	{
		[self ComicOver];
	}
}

- (void)CallBackPage3FadeInOver:(id)Sender
{
	id _FadeIn;

	if (activePageCount_ < 5)
	{
		_FadeIn = [CCFadeIn actionWithDuration:COMIC_PIC_FADE_IN_TIME];
		[page_3_[activePageCount_] runAction:[CCShow action]];
		[page_3_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage3FadeInOver:))];
		activePageCount_++;
	}
	else
	{
		[self ComicOver];
	}
}

- (void)CallBackPage4FadeInOver:(id)Sender
{
	id _FadeIn;

	if (activePageCount_ < 7)
	{
		_FadeIn = [CCFadeIn actionWithDuration:COMIC_PIC_FADE_IN_TIME];
		[page_4_[activePageCount_] runAction:[CCShow action]];
		[page_4_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage4FadeInOver:))];
		activePageCount_++;
	}
	else
	{
		[self ComicOver];
	}
}

- (void)CallBackPage5FadeInOver:(id)Sender
{
	id _FadeIn;

	if (activePageCount_ < 4)
	{
		_FadeIn = [CCFadeIn actionWithDuration:COMIC_PIC_FADE_IN_TIME];
		[page_5_[activePageCount_] runAction:[CCShow action]];
		[page_5_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage5FadeInOver:))];
		activePageCount_++;
	}
	else
	{
		[self ComicOver];
	}
}

- (void)CallBackPage6FadeInOver:(id)Sender
{
	id _FadeIn;

	if (activePageCount_ < 4)
	{
		_FadeIn = [CCFadeIn actionWithDuration:COMIC_PIC_FADE_IN_TIME];
		[page_6_[activePageCount_] runAction:[CCShow action]];
		[page_6_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage6FadeInOver:))];
		activePageCount_++;
	}
	else
	{
		[self ComicOver];
	}
}

- (void)CallBackPage7FadeInOver:(id)Sender
{
	id _FadeIn;

	if (activePageCount_ < 4)
	{
		_FadeIn = [CCFadeIn actionWithDuration:COMIC_PIC_FADE_IN_TIME];
		[page_7_[activePageCount_] runAction:[CCShow action]];
		[page_7_[activePageCount_] runAction:_FadeIn];
		[self runAction:tcCreateActionCallBack(self, COMIC_PIC_WAIT_TIME, @selector(CallBackPage7FadeInOver:))];
		activePageCount_++;
	}
	else
	{
		[self ComicOver];
	}
}

- (void)CallBackSkip:(id)Sender
{
	if (flagFunctionLock_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	[self ComicOver];
}

@end